package kz.kstu.ds.generate;

import kz.kstu.ds.chooser.KeySaveChooser;
import kz.kstu.ds.entity.User;
import kz.kstu.ds.key.KeyGenerator;
import kz.kstu.ds.utils.GUIFactory;
import kz.kstu.ds.utils.GUIIcon;
import kz.kstu.ds.utils.NumberUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;

/**
 * Date: 03.11.12
 * Time: 22:46
 *
 * @author Oleg Anastassov
 */
public class GenerateKeyFrame extends JFrame {

    private static final String TITLE = "Создание ключей";

    private static final int HEIGHT = 300;
    private static final int WIDTH = 400;

    private static final int PASSPORT_FIELD_LENGTH = 7;

    private JTextField surnameTextField;
    private JTextField nameTextField;
    private JTextField patronymicTextField;
    private JTextField passportNoTextField;
    private JButton generateButton;
    private JPanel rootPanel;

    public GenerateKeyFrame() {
        setTitle(TITLE);
        setSize(new Dimension(WIDTH, HEIGHT));
        setResizable(false);
        setContentPane(rootPanel);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        initIcons();
        initListeners();
    }

    private void initIcons() {
        setIconImage(GUIFactory.getImage(GUIIcon.LOGO));
        generateButton.setIcon(GUIFactory.getIcon(GUIIcon.KEY));
    }

    private void initListeners() {
        surnameTextField.addKeyListener(new ValidateKeyAdapter());
        nameTextField.addKeyListener(new ValidateKeyAdapter());
        patronymicTextField.addKeyListener(new ValidateKeyAdapter());
        passportNoTextField.addKeyListener(new ValidateKeyAdapter());
        generateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                KeySaveChooser fc = new KeySaveChooser();
                int result = fc.showSaveDialog(rootPanel);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File dir = fc.getSelectedFile();

                    KeyGenerator kg = new KeyGenerator();
                    kg.saveKeys(getUser(), dir);

                    JOptionPane.showMessageDialog(rootPanel, "Открытый и закрытый ключи успешно созданы.");
                }
            }
        });
    }

    private User getUser() {
        User user = new User();
        user.setSurname(surnameTextField.getText());
        user.setName(nameTextField.getText());
        user.setPatronymic(patronymicTextField.getText());
        user.setPassportNo(passportNoTextField.getText());
        return user;
    }

    private boolean isValidForm() {
        return surnameTextField.getText().length() > 0
                && nameTextField.getText().length() > 0
                && patronymicTextField.getText().length() > 0
                && NumberUtil.isLongNumber(passportNoTextField.getText())
                && passportNoTextField.getText().length() == PASSPORT_FIELD_LENGTH;
    }

    private class ValidateKeyAdapter extends KeyAdapter {
        @Override
        public void keyReleased(KeyEvent e) {
            generateButton.setEnabled(isValidForm());
        }
    }

}
